<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="utf-8">
    <title>角色管理</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" th:href="@{/layuimini/lib/layui-v2.5.5/css/layui.css}"
          href="../../../static/layuimini/lib/layui-v2.5.5/css/layui.css" media="all">
    <link rel="stylesheet" th:href="@{/layuimini/css/public.css}" href="../../../static/layuimini/css/public.css"
          media="all">
    <style>
        body .layui-layer-btn {
            background-color: #F8F8F8;
            border-top: 1px solid #eee;
            padding-top: 10px;
        }
    </style>
</head>
<body>
<div class="layuimini-container">
    <div class="layuimini-main">

        <form class="layui-form" lay-filter="tableToolForm">
            <div class="layui-form-item">
                <label class="layui-form-label" style="width: 42px;padding-left: 0px;">角色名</label>
                <div class="layui-inline">
                    <input id="roleName" class="layui-input" name="roleName" type="text" placeholder="输入关键字">
                </div>

                <div class="layui-inline">
                    <button class="layui-btn" id="btn-search">
                        <i class="layui-icon">&#xe615;</i>搜索
                    </button>
                    <button class="layui-btn layui-btn-primary" id="btn-reload">
                        <i class="layui-icon">&#xe666;</i>清空搜索
                    </button>
                    <button class="layui-btn layui-btn-normal" id="btn-add">
                        <i class="layui-icon">&#xe624;</i>新增
                    </button>
                    <button class="layui-btn layui-btn-danger" id="btn-delete">
                        <i class="layui-icon">&#xe640;</i>删除
                    </button>
                </div>
            </div>
        </form>

        <table class="layui-hide" id="currentTableId" lay-filter="currentTableFilter"></table>

        <script type="text/html" id="currentTableBar">
            <a class="layui-btn layui-btn-normal layui-btn-xs data-count-edit" lay-event="edit">编辑</a>
            <a class="layui-btn layui-btn-xs layui-btn-danger data-count-delete" lay-event="delete">删除</a>
        </script>

    </div>
</div>
<script th:src="@{/layuimini/lib/layui-v2.5.5/layui.js}" src="../../../static/layuimini/lib/layui-v2.5.5/layui.js"
        charset="utf-8"></script>
<script>
    layui.use(['form', 'table'], function () {
        var $ = layui.jquery,
            form = layui.form,
            table = layui.table;

        var userTable = table.render({
            elem: '#currentTableId',
            url: '[[@{/}]]manage/role/list',
            defaultToolbar: ['filter', 'exports', 'print', {
                title: '提示',
                layEvent: 'LAYTABLE_TIPS',
                icon: 'layui-icon-tips'
            }],
            cols: [
                [
                    {type: "checkbox", minWidth: 50},
                    {field: 'roleId', minWidth: 80, title: 'ID'},
                    {field: 'roleName', minWidth: 120, title: '角色名'},
                    {
                        title: '操作', minWidth: 150, align: "center", templet: function (d) {
                            var btn = '';
                            if (d.roleId == 1) {
                                btn = '<button class="layui-btn layui-btn-xs layui-btn-disabled layui-btn-radius" disabled lay-event="edit"><i class="layui-icon">&#xe642;</i>编辑</button>' +
                                    '<button class="layui-btn layui-btn-xs layui-btn-disabled layui-btn-danger layui-btn-radius" disabled lay-event="delete"><i class="layui-icon">&#xe640;</i>删除</button>' +
                                    '<button class="layui-btn layui-btn-xs layui-btn-disabled layui-btn-normal layui-btn-radius" disabled lay-event="role-permission"><i class="layui-icon">&#xe640;</i>授权</button>';
                            }else{
                                btn = '<button class="layui-btn layui-btn-xs  layui-btn-radius" lay-event="edit"><i class="layui-icon">&#xe642;</i>编辑</button>' +
                                    '<button class="layui-btn layui-btn-xs layui-btn-danger layui-btn-radius" lay-event="delete"><i class="layui-icon">&#xe640;</i>删除</button>' +
                                    '<button class="layui-btn layui-btn-xs layui-btn-normal layui-btn-radius" lay-event="role-permission"><i class="layui-icon">&#xe640;</i>授权</button>';
                            }
                            return btn;
                        }
                    }
                ]
            ],
            limits: [10, 15, 20, 25, 50, 100],
            limit: 15,
            page: true,
            skin: 'line'
        });

        form.on('submit(tableToolForm)', function (data) {
            return false;
        });

        //搜索
        $('#btn-search').click(function () {
            var roleName = $('#roleName').val();

            if (roleName.trim().length == 0) {
                layer.msg('请输入要搜索的内容');
                return;
            }

            // userTable.reload({
            table.reload('currentTableId', {
                where: { //设定异步数据接口的额外参数，任意设
                    roleName: roleName
                }
                , page: {
                    curr: 1 //重新从第 1 页开始
                }
                , done: function () {
                    this.where = {};//清空查询参数
                }
            }); //只重载数据
        });
        //清空搜索
        $('#btn-reload').click(function () {
            $('#roleName').val('');
            table.reload('currentTableId', {
                page: {
                    curr: 1 //重新从第 1 页开始
                }
            }); //只重载数据
        });
        //新增
        $('#btn-add').click(function () {
            layer.open({
                type: 2
                , title: '新增角色'
                , content: '[[@{/}]]manage/role/role-edit.html'
                , resize: false
                , area: ['500px', '170px']
                , end: function () {
                    location.reload();
                }
            });
        });

        //批量删除
        $('#btn-delete').click(function () {
            var dataList = table.checkStatus('currentTableId').data;
            var ids = [];
            var canDelete=true;
            $(dataList).each(function () {
                if (this.roleId==1){
                    canDelete=false;
                    return false;
                }
                ids.push(this.roleId);
            });
            if (!canDelete){
                layer.msg('默认角色不能删除');
                return;
            }
            console.log(ids);
            layer.confirm('真的删除么', function (index) {
                $.ajax({
                    type: 'get',
                    url: '[[@{/}]]manage/role/delete',
                    data: {roleIds: ids},
                    traditional: true,//传统方式传递数组
                    dataType: 'json',
                    success: function (result) {
                        console.log(result);
                        if (result.success) {
                            layer.msg("操作成功", {shift: -1, time: 500}, function () {
                                table.reload('currentTableId');
                            });

                        } else {
                            layer.msg(result.msg);
                        }
                    },
                    error: function (jqXHR, textStatus, errorThrown) {
                        layer.msg(textStatus);
                    },
                    complete: function () {
                        layer.closeAll('loading');
                    }
                });
            });

        });

        //监听表格复选框选择
        table.on('checkbox(currentTableFilter)', function (obj) {
            /*console.log(obj)
            if (obj.data.username == 'admin') {
                layer.msg('管理员不可操作');
                obj.checked = false;
                return false;
            }*/

        });

        table.on('tool(currentTableFilter)', function (obj) {
            var data = obj.data;
            if (obj.event === 'edit') {
                layer.open({
                    type: 2
                    , title: '修改角色 [id:' + data.roleId + ']'
                    , content: '[[@{/}]]manage/role/role-edit.html?roleId=' + data.roleId
                    , resize: false
                    , area: ['500px', '170px']
                    , end: function () {
                        location.reload();
                    }
                });
            } else if (obj.event === 'delete') {
                layer.confirm('真的删除么', function (index) {
                    $.ajax({
                        type: 'get',
                        url: '[[@{/}]]manage/role/delete',
                        data: {roleId: data.roleId},
                        dataType: 'json',
                        success: function (result) {
                            console.log(result);
                            if (result.success) {
                                layer.msg("操作成功");
                                obj.del();
                            } else {
                                layer.msg(result.msg);
                            }
                        },
                        error: function (jqXHR, textStatus, errorThrown) {
                            layer.msg(textStatus);
                        },
                        complete: function () {
                            layer.closeAll('loading');
                        }
                    });

                    layer.close(index);
                });
            } else if (obj.event === 'role-permission') {
                layer.open({
                    type: 2
                    , title: '角色关联权限 [id:' + data.roleId + ']'
                    , content: '[[@{/}]]manage/role/role-permission.html?roleId=' + data.roleId+'&roleIn=true'
                    , resize: false
                    , area: ['650px', '581px']
                    , btn:['添加权限','取消']
                    , yes:function () {
                        layer.open({
                            type: 2
                            , title: '角色添加权限 [id:' + data.roleId + ']'
                            , content: '[[@{/}]]manage/role/role-permission-select.html?roleId=' + data.roleId+'&roleIn=false'
                            , resize: false
                            , area: ['650px', '581px']
                            , btn:['设置选中','取消']
                            , yes:function (index,layero) {
                                //设置选中，获取选中的perId
                                var iframeWindow = window['layui-layer-iframe'+ index];
                                var select = iframeWindow.getSelect();
                                var ids = [];
                                $(select).each(function () {
                                    ids.push(this.authorityId);
                                });
                                console.log(ids);
                                $.ajax({
                                    type: 'post',
                                    url: '[[@{/}]]manage/role/roleAddPermission',
                                    data: {roleId:data.roleId,permissionIds:ids},
                                    dataType: 'json',
                                    traditional: true,//传统方式传递数组
                                    success: function (result) {
                                        console.log(result);
                                        if (result.success){
                                            layer.msg("保存成功",{ shift: -1, time: 500 },function () {
                                                window['layui-layer-iframe'+ 1].location.reload();
                                                layer.close(index);
                                            });

                                        }else{
                                            layer.msg(result.msg);
                                        }
                                    },
                                    error: function (jqXHR, textStatus, errorThrown) {
                                        layer.msg(textStatus);
                                    }
                                });
                            }
                            , btn2:function (index,layero) {
                                //打开的第一个iframe页作为刷新页
                                var iframeWindow = window['layui-layer-iframe'+ 1];
                                iframeWindow.location.reload();
                                // iframeWindow.tableTree.reload();
                                layer.close(index);

                            }
                            , cancel:function (index) {
                                var iframeWindow = window['layui-layer-iframe'+ 1];
                                iframeWindow.location.reload();
                                layer.close(index);
                            }
                        });
                    }
                    , btn2:function (index,layero) {
                        layer.close(index);
                    }
                    , end: function () {
                        location.reload();
                    }
                });
            }
        });

    });
</script>

</body>
</html>